package com.groupfifteen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupfifteen.common.R;
import com.groupfifteen.entity.SysExamManageEntity;
import com.groupfifteen.entity.SysUserEntity;
import com.groupfifteen.service.SysExamManageService;
import com.groupfifteen.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("sysexam/manage")
public class SysExamManageController {

    @Autowired
    private SysExamManageService sysExamManageService;

    @PostMapping("/query")
    public IPage<SysExamManageEntity> QuerySysExamManages(@RequestBody SysExamManageEntity sysExamManageEntity,
                                                          @RequestParam(defaultValue = "1") int pageNo,
                                                          @RequestParam(defaultValue = "10") int pageSize) {
        return sysExamManageService.QuerySysExamManages(sysExamManageEntity, pageNo, pageSize);
    }

    @PostMapping("addexam")
    public String AddExam(@RequestBody SysExamManageEntity sysExamManageEntity) {
        Integer result = sysExamManageService.AddExam(sysExamManageEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    //souy
    @DeleteMapping("/deleteexam/{exam_code}")
    public String DeleteExam(@PathVariable("exam_code") Integer exam_code) {
        Integer result = sysExamManageService.DeleteExam(exam_code);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    @GetMapping("/getexam/{exam_code}")
    public SysExamManageEntity GetExam(@PathVariable("exam_code") Integer exam_code) {
        SysExamManageEntity result = sysExamManageService.GetExam(exam_code);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    @PutMapping("/updateexam/{exam_code}")
    public String UpdateExam(@PathVariable("exam_code")Integer exam_code,@RequestBody SysExamManageEntity sysExamManageEntity) {
        sysExamManageEntity.setExamCode(exam_code);
        Integer result = sysExamManageService.UpdateExam(sysExamManageEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    /**
     * 查询所有考试
     *
     * @return 考试列表
     */
    @GetMapping("/exams")
    public R<List<SysExamManageEntity>> findAll() {
        return ApiResultHandler.buildApiResult(200, "请求成功！", sysExamManageService.findAll());
    }

    /**
     * 查询考试 分页
     *
     * @param page 分页页数
     * @param size 分页大小
     * @return 考试列表
     */
    @GetMapping("/exams/{page}/{size}")
    public R<IPage<SysExamManageEntity>> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ApiResultHandler.buildApiResult(200, "请求成功！", sysExamManageService.findAll(new Page<>(page, size)));
    }

    /**
     * 根据考试编号查询考试信息
     *
     * @param examCode 考试编号
     * @return 考试信息
     */
    @GetMapping("/exam/{examCode}")
    public R<SysExamManageEntity> findById(@PathVariable("examCode") Integer examCode) {
        SysExamManageEntity res = sysExamManageService.findById(examCode);
        if (res == null) {
            return ApiResultHandler.buildApiResult(10000, "考试编号不存在", null);
        }
        return ApiResultHandler.buildApiResult(200, "请求成功！", res);
    }

    /**
     * 根据考试编号删除
     *
     * @param examCode 考试编号
     */
    @DeleteMapping("/exam/{examCode}")
    public R deleteById(@PathVariable("examCode") Integer examCode) {
        return ApiResultHandler.buildApiResult(200, "删除成功", sysExamManageService.delete(examCode));
    }

    /**
     * 根据考试编号更新
     *
     * @param examManage 考试信息
     */
    @PutMapping("/exam")
    public R update(@RequestBody SysExamManageEntity examManage) {
        return ApiResultHandler.buildApiResult(200, "更新成功", sysExamManageService.update(examManage));
    }

    /**
     * 添加考试信息
     *
     * @param examManage 考试信息
     */
    @PostMapping("/exam")
    public R add(@RequestBody SysExamManageEntity examManage) {
        int res = sysExamManageService.add(examManage);
        if (res == 1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "添加失败", res);
        }
    }

    /**
     * 查询最后一条记录的paperId,返回给前端达到自增效果
     *
     * @return 最后一条记录
     */
    @GetMapping("/examManagePaperId")
    public R<SysExamManageEntity> findOnlyPaperId() {
        SysExamManageEntity res = sysExamManageService.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200, "请求成功", res);
        }

        return ApiResultHandler.buildApiResult(400, "请求失败", res);
    }
}